package com.terrace.controller.system;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.terrace.core.common.model.ResultData;
import com.terrace.db.system.mapper.entity.DictData;
import com.terrace.db.system.service.DictDataService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.core.utils.UUIDGenerator;
import com.terrace.web.annotation.Logs;
import com.terrace.web.constant.GlobalsConstant;
import com.terrace.web.model.DataGrid;

/**
 * 字典值
 * @author jiangyg
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/dictData")
public class DictDataController {
	
	private static final String MESSAGE_TYPE = "字典值";
	
	@Autowired
	private DictDataService dictDataService;
	
	/**
	 * 默认管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "dictDataList")
	public ModelAndView dictDataList(@RequestParam(value = "dictId") String dictId ,HttpServletRequest request){
		request.setAttribute("dictId", dictId);
		return new ModelAndView("system/dict/dictDataList");
	}
	
	/**
	 * 数据表格
	 * @param request
	 * @param pagination
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "datagrid")
	@RequiresPermissions("system:dictdata:view")
	public DataGrid<DictData> datagrid(DictData dictData, Pagination<DictData> pagination, HttpServletRequest request) {
		DataGrid<DictData> dataGrid = new DataGrid<DictData>();
		dataGrid.setPagination(dictDataService.selectDictDatasBySelective(dictData, pagination));
		return dataGrid;
	}
	
	/**
	 * 新增或者修改页面
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(@RequestParam(value = "dictId") String dictId, String id, HttpServletRequest request){
		if (StringUtils.isNotBlank(id)) {
			DictData dictData = dictDataService.selectByDictDataId(id);
			request.setAttribute("dictData", dictData);
		}
		request.setAttribute("dictId", dictId);
		return new ModelAndView("system/dict/dictData");
	}
	
	/**
	 * 保存数据字典
	 * @param dict
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequestMapping(params = "saveOrUpdate")
	@RequiresPermissions("system:dictdata:save")
	@Logs(depict = "保存数据字典数据", type = GlobalsConstant.LogTypes.SYS_DICT)
	public ResultData saveOrUpdate(DictData dictData, HttpServletRequest request) {
		ResultData rd = new ResultData();
		if (StringUtils.isBlank(dictData.getId())) {
			if (dictDataService.selectDictDataRepeat(dictData)) {
				return rd.failObjs("13104", MESSAGE_TYPE, dictData.getDataValue());
			}
			dictData.setId(UUIDGenerator.generate());
			dictDataService.saveDictDataSelective(dictData);
			rd.successObjs("10001", MESSAGE_TYPE);
		} else {
			DictData temp = dictDataService.selectByDictDataId(dictData.getId());
			if (temp == null) {
				return rd.failObjs("13103", MESSAGE_TYPE);
			}
			dictDataService.updateByDictDataIdSelective(dictData);
			rd.successObjs("10002", MESSAGE_TYPE);
		}
		return rd;
	}
	
	/**
	 * 删除数据字典
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "delete")
	@RequiresRoles(GlobalsConstant.Roles.ADMIN)
	@RequiresPermissions("system:dictdata:delete")
	@Logs(depict = "删除数据字典数据", type = GlobalsConstant.LogTypes.SYS_DICT)
	public ResultData delete(@RequestParam(value = "id") String dictDataId, HttpServletRequest request) {
		ResultData rd = new ResultData();
		DictData dict = dictDataService.selectByDictDataId(dictDataId);
		if (dict == null) {
			return rd.failObjs("13103", MESSAGE_TYPE);
		}
		int result = dictDataService.deleteByDictDataId(dictDataId);
		if (result != 1) {
			rd.failObjs("13102", MESSAGE_TYPE);
		} else {
			rd.successObjs("10003", MESSAGE_TYPE);
		}
		return rd;
	}

}
